#include "select.hpp"

namespace webgen{
option::option():
m_is_default(false),
m_element(xhtml_gen::option)
{}

option::option(const std::string& value, const std::string& content):
m_value(value),
m_content(content),
m_is_default(false),
m_element(xhtml_gen::option)
{}

xhtml_gen::element& option::get_element()
{
	m_element.set_attribute(xhtml_gen::value,m_value);
	if(m_is_default)
		m_element.set_attribute(xhtml_gen::selected,"selected");

	m_element << m_content;

	return m_element;
}

select::select()
{}

select::select(const std::string& name, const std::string& css)
{
	m_name = name;
	m_css = css;
}

xhtml_gen::element& select::get_element()
{
	m_element = xhtml_gen::element(xhtml_gen::select);
	m_element.set_attribute(xhtml_gen::name,m_name);
	m_element.set_attribute(xhtml_gen::id,m_name);
	m_element.set_attribute(xhtml_gen::classid,m_css);

	for(unsigned int i=0;i<m_option.size();i++){
		if(m_default == m_option[i].m_value){
			m_option[i].m_is_default = true;					
		}
		m_element << m_option.at(i).get_element();
	}

	return m_element;
}

void select::add_item(const std::string& value, const std::string& content)
{
	m_option.push_back(option(value,content));
}

void select::del_item(const std::string& value)
{

}

void select::set_default(const std::string& value)
{
	m_default = value;
}
}
